<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类管理</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="__PUBLIC__/Theme1/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__PUBLIC__/Theme1/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="__PUBLIC__/Theme1/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="__PUBLIC__/Theme1/css/animate.min.css" rel="stylesheet">
    <link href="__PUBLIC__/Theme1/css/style.min.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>添加(修改)菜品
                        <small></small>
                    </h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="form_basic.html#">
                            <i class="fa fa-wrench"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form method="post" action="__URL__/editGood" class="form-horizontal" enctype="multipart/form-data">
                        <input type="hidden" value="{$result.id}" name="id">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜品名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="name" id="name" placeholder="请输入菜品名称" value="{$result.name}" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜品描述</label>
                            <div class="col-sm-10">
                                <input type="tel" name="description" id="description" placeholder="菜品描述"
                                       value="{$result.description}" class="form-control" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜品食材</label>
                            <div class="col-sm-10">
                                <input type="tel" name="food_material" id="food_material" placeholder="菜品食材"
                                       value="{$result.food_material}" class="form-control" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">库存</label>
                            <div class="col-sm-10">
                                <input type="text" name="num" id="num" placeholder="请输入库存"
                                       value="{$result.num}" class="form-control" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜品单价</label>
                            <div class="col-sm-10">
                                <input type="text" name="price" id="price" placeholder="请输入菜品价格"
                                       value="{$result.price}" class="form-control" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜品原价</label>
                            <div class="col-sm-10">
                                <input type="text" name="original_price" id="original_price" placeholder="请输入菜品原价"
                                       value="{$result.original_price}" class="form-control" required>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否上架</label>
                            <div class="col-sm-2">
                                <select class="form-control" required name="is_up">
                                    <option value="1"
                                    <if condition="$result['is_up'] eq 1">selected</if>
                                    >上架</option>
                                    <option value="2"
                                    <if condition="$result['is_up'] eq 2">selected</if>
                                    >下架</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">图片:</label>
                            <div class="col-sm-3">
                                <div class="z_photo upimg-div clear">
                                    <strong>点击图片删除</strong>
                                    <div id="preview" onclick="uploadpic()"></div>
                                    <if condition="($result.image neq '')">
                                        <div id='wxpic' onclick="uploadpic()">
                                            <img class="re"
                                                 style="padding: 0px 0px 6px 0px;max-width:200px;max-height:200px;"
                                                 src="__PUBLIC__{$result.image}"/>
                                            <input type="hidden" class="addindeximage" name="addindeximage"
                                                   value="{$result.image}">
                                        </div>
                                        <else/>
                                        <img class="re"
                                             style="padding: 0px 0px 6px 0px;max-width:200px;max-height:200px;"
                                             class="addindeximage" name="addindeximage" src=""/>
                                    </if>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label"></label>
                            <div class="col-sm-5">
                                <span class="btn btn-primary">上传</span>
                                <input type="file"
                                       style="width: 55px;position: relative;left: 0;z-index: 15;height: 35px;top: -30px;opacity: 0;float: none;"
                                       name="img" value="" id="img" onchange="previewImage(this)">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜品分类</label>
                            <div id="category" class="col-sm-2">
                                    <select class="form-control" required name="category_id">
                                        <volist name="category" id="vo">
                                            <option value="{$vo['id']}" <if condition="$vo['id'] eq $result['category_id']">selected="selected"</if>>{$vo['name']}</option>
                                        </volist>
                                    </select>
                            </div>
                        </div>

                        <if condition="$storeId eq 0">
                            <div class="form-group">
                                <label class="col-sm-2 control-label"> 店铺名称 </label>
                                <div class="col-sm-2">
                                    <select id="select_store" class="form-control" required name="store_id" onchange="changStoreId(this.value)">
                                        <volist name="store" id="vo">
                                            <option value="{$vo['store_id']}" <if condition="$vo['store_id'] eq $result['store_id']">selected="selected"</if>>{$vo['store_name']}</option>
                                        </volist>
                                    </select>
                                </div>
                            </div>
                        </if>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="submit">保存</button>
                                <a class="btn btn-white" href="{:U('Admin/Good/index')}">返回</a>
                            </div>
                        </div>
                        <input type="hidden" value="{$result.image}" id="images">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="__PUBLIC__/Theme1/js/jquery.min.js?v=2.1.4"></script>
<script src="__PUBLIC__/Theme1/js/bootstrap.min.js?v=3.3.6"></script>
<script src="__PUBLIC__/Theme1/js/content.min.js?v=1.0.0"></script>
<script src="__PUBLIC__/Theme1/js/plugins/iCheck/icheck.min.js"></script>
<script>
    $(document).ready(function () {
        $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green",})
        var images = $("#images").val();
        var pic_arr = images.split(',');

        if (pic_arr.length > 1) {
            for (var i in pic_arr) {
                $('input[name="img[]"]').parent().append('<img src="__PUBLIC__' + pic_arr[i] + '" style="max-width:100px;height: 145px" class="img-thumbnail">');
            }
        }
        var store_id="{$result.store_id}";
        // alert(store_id)
        //加载店铺菜品分类
        changStoreId(store_id);
    });

    function previewImage(file) {
        var prevDiv = document.getElementById('preview');
        if (file.files && file.files[0]) {
            var reader = new FileReader();
            reader.onload = function (evt) {
                prevDiv.innerHTML = '<img style="width: 200px;height: 200px" class="re" src="' + evt.target.result + '" />';
                $("#wxpic").next("input").remove();
                $("#wxpic").remove();
            }
            reader.readAsDataURL(file.files[0]);
        } else {
            prevDiv.innerHTML =
                '<div class="re" style="width: 200px;height: 200px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
            $("#wxpic").next("input").remove();
            $("#wxpic").remove();
        }
    }

    function uploadpic() {
        $("#wxpic").next("input").remove();
        $("#wxpic").remove();
        $(".re").attr("src", "");
        $(".re").attr("style", "display:none");
    };

    //如果是超级管理员添加菜品选择对应的店铺对应的菜品分类
    function changStoreId(store_id) {
        $.ajax({
            // url:'__CONTROLLER__/ajax_storeCategory/',   //请求的url地址
            url: '{:U('/Admin/Good/ajax_storeCategory')}',   //请求的url地址
            dataType: "json",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            type: "GET",   //请求方式
            data: {
                store_id: store_id
            },
            success: function (data) {
                if (data.state == 1) {
                    console.log(data.data)
                    var html = '';
                    html += '<select class="form-control" required name="category_id">'
                    $.each(data.data, function (key, value) {
                        html += '<option value=' + key + '>' + value + '</option>'
                    });
                    html += '</select>'

                    $("#category").html(html);
                }
            }
        });
    }
</script>
</body>

</html>
